<template>
  <div class='container'>
      <el-dialog
        width="40%"
        @close="closeDailog"
        :close-on-click-modal = false
        title="题目预览"
        :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-radio-group v-model="form.chkState" class="radioTp">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">不通过</el-radio>
        </el-radio-group>
        <el-form-item prop="chkRemarks">
          <el-input type="textarea" placeholder="请输入审核意见" v-model="form.chkRemarks"></el-input>
        </el-form-item>
        <el-form-item class="btnRg">
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { choiceCheck } from '@/api/hmmm/questions.js'
export default {
  name: 'questions-check',
  props: {
    value: {
      type: Boolean
    },
    id: {
      type: Number
    }
  },
  data () {
    return {
      form: {
        chkState: 1,
        chkRemarks: ''
      },
      rules: {
        chkRemarks: [
          { required: true, message: '请输入审核意见', trigger: 'blur' }
        ]
      },
      // 是否弹出
      dialogVisible: this.value
    }
  },
  // 生命周期 - vue实例创建完成（访问当前this实例）
  created () {},
  // 生命周期 - dom挂载完成（访问DOM元素）
  mounted () {},
  // 事件处理器
  methods: {
    // 点击确认按钮
    onSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.setCheck()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 调用接口封装方法
    async setCheck () {
      try {
        const res = await choiceCheck({ ...this.form, id: this.id })
        console.log(res)
        this.$message.success('操作成功')
        // 发布成功发布事件让精品题库进行刷新
        this.$eventBus.$emit('refQuestionList')
        this.dialogVisible = false
      } catch (error) {
        console.log('审核失败', error)
        this.$message.error('操作失败')
      }
    },
    // 关闭弹层时触发的事件---------------------------------------
    closeDailog () {
      this.$emit('input', false)
    }
  },
  watch: {
    value: function (newVal) {
      this.form.chkRemarks = ''
      this.dialogVisible = newVal
    }
  }
}
</script>

<style lang='less' scoped>
.radioTp{
  padding: 20px 0;
}
.btnRg{
  text-align: right;
}
</style>
